<!doctype html>
<html lang="zh-CN">
<head>
    <#include "common/head.html"/>
    <title>秒杀详情页</title>
</head>
<body>
    <div class="container">
        <div class="pannel pannel-default text-center">
            <div class="pannel-heading">
                <h1>产品详情：${name}</h1>
            </div>
            <div class="pannel-body">
                <h2>剩余时间: <span id="clock">${subTime}</span> 秒</h2>
                <button id="buyBtn" style="visibility: hidden;" class="btn-primary btn-lg" onclick="startSeckill()">开始秒杀</button>
            </div>
        </div>
    </div>


<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
<script type="text/javascript">
    function seckill(random) {
        var reqData = {
            randomStr: random + '',
            userId: '123'
        };
        $.ajax({
            url:'http://localhost:80/seckill/' + ${id},
            type : 'post',
            data : JSON.stringify(reqData),
            contentType: "application/json",
            dataType : 'json',//返回的数据格式
            success : function(data){ //请求成功的回调函数
                console.log(data);
                alert(data.msg);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest.status);
                console.log(XMLHttpRequest.readyState);
                console.log(textStatus);

            }
        });
    }
    function startSeckill() {
        $.ajax({
            url:'http://localhost:80/export/' + ${id},  //请求的URL
            type : 'post',  //请求方式，get或post
            data : {
            },  //请求所传参数，json格式
            dataType : 'json',//返回的数据格式
            success : function(data){ //请求成功的回调函数
                seckill(data);
            }
        });
    }
    // 秒杀过程
    function skillProcess() {
        console.log('执行秒杀逻辑，并返回');
        $("#buyBtn").css("visibility","visible");
        clearTimeout(interval);
    }
    var time = ${subTime};
    var interval = null;
    $(function () {
        interval = setInterval(function() {
            if(time <= 0) {
               skillProcess();
            }
            time --;
            $('#clock').text(time);
        },1000);
    });
</script>
</body>
</html>